<template>
  <div>
    <div class="order-dropdown-box">
      <span>时间：</span>
      <el-dropdown  @command="handleTimeCommand" trigger="click">
        <span class="xz-dropdown-link">
          {{selectTimeItem.name}}<svg-icon iconClass="triangle"/>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-for="item in timeList" :key="item.name" :command="item" :class="{active:selectTimeItem.name===item.name}">{{item.name}}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <span style="margin-left:20px;">状态：</span>
      <el-dropdown  trigger="click" @command="handleStatusCommand">
        <span class="xz-dropdown-link">
          {{selectStatusItem.name}}<svg-icon iconClass="triangle"/>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-for="item in statusList" :key="item.name" :command="item" :class="{active:selectStatusItem.name===item.name}">{{item.name}}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-table :data="tableData"
      v-loading="listLoading"
      @row-contextmenu="rightClick"
      :height="tableHeight"
      class="order-table">
      <el-table-column
        align="center"
        header-align="center"
        prop="order_id"
        label="订单号"
        min-width="155">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="title"
        label="商品"
        min-width="120">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="订单金额"
        min-width="70">
        <template slot-scope="scope">
          {{parseFloat(scope.row.pay_price/100)}}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="pay_price"
        label="下单时间"
        min-width="115">
        <template slot-scope="scope">
          {{scope.row.created_at|parseTime('{y}-{m}-{d} {h}:{i}')}}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label-class-name="order-last-header"
        label="状态"
        min-width="60">
        <template slot-scope="scope">
          {{scope.row.status|statusFilter}}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      small
      hide-on-single-page
      class="order-pagination"
      :pager-count="5"
      layout="prev, pager, next"
      :page-size="100"
      :current-page.sync="page"
      :total="total"
      @current-change="getList">
    </el-pagination>
    <div class="right-click-box" id="rightClickBox" v-if="rightRowInfo.visible" :style="{ left: rightRowInfo.left + 'px', top: rightRowInfo.top + 'px' }">
      <el-button @click="invoiceClick('select')">{{ rightRowInfo.row.invoice_status === 3 ? '发票记录' : '申请发票' }}</el-button>
    </div>
    <el-button type="text" class="invoice-btn-bottom" @click="invoiceClick('false')">申请发票</el-button>

    <!-- 申请发票弹窗 -->
    <invoiceFromDialog :row="invoiceForm" :visibleShow.sync="showInvoiceDialog" @close="close"/>
    <!-- 合作方提示弹窗 -->
    <invoiceContactDialog :showContactVisible.sync="showContactDialog" @close="closeContact"/>
    <!-- 发票记录弹窗 -->
    <invoiceRecordDialog :showRecordVisible.sync="showRecordDialog" @close="closeRecordDialog"/>
  </div>
</template>
<script>
import invoiceFromDialog from './invoice-from-dialog.vue'
import invoiceRecordDialog from './invoice-record-dialog.vue'
import invoiceContactDialog from './invoice-contact-dialog.vue'
import { orderListApi } from '@/api/order'
export default {
  name: 'OrderTable',
  components: {
    invoiceFromDialog,
    invoiceRecordDialog,
    invoiceContactDialog
  },
  props: {
    tableHeight: {
      type: String,
      default: '100%'
    },
    timeList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      showInvoiceDialog: false,
      showContactDialog: false,
      showRecordDialog: false,
      listLoading: false,
      selectTimeItem: { name: '全部' },
      selectStatusItem: { name: '已支付', value: 2 },
      statusList: [
        { id: 1769, name: '全部', value: null },
        { id: 1770, name: '待支付', value: 1 },
        { id: 1771, name: '已支付', value: 2 },
        { id: 1772, name: '支付超时', value: 5 }
      ],
      page: 1,
      total: 0,
      tableData: [],
      rightRowInfo: {
        visible: false, // 默认隐藏右键按钮
        left: 0,
        top: 0,
        row: {}// el-table当前列表的信息
      },
      invoiceForm: {}
    }
  },
  filters: {
    statusFilter (val) {
      const arr = ['', '待支付', '已支付', '支付失败', '已取消', '支付超时', '已退款']
      return arr[val]
    }
  },
  activated () {
    const query = this.$route.query
    if (query.status === '2') {
      this.selectTimeItem = { name: '全部' }
      this.selectStatusItem = { name: '已支付', value: 2 }
    }
    this.getList()
  },
  created () {
    // this.getList()
  },
  methods: {
    getList () {
      const params = {
        page: this.page,
        page_size: 100,
        product_type: 0,
        product_array: '2,6'
      }
      if (this.selectTimeItem.begin_date) {
        params.begin_date = this.selectTimeItem.begin_date
        params.end_date = this.selectTimeItem.end_date
      }
      if (this.selectStatusItem.value) {
        params.status = this.selectStatusItem.value
      }
      this.listLoading = true
      orderListApi(params).then(res => {
        const data = res.data || {}
        this.tableData = data.list
        this.total = data.count
      }).finally(() => {
        this.listLoading = false
      })
    },
    handleTimeCommand (command) {
      this.$xz('xz_substat_action', command.orderId)
      this.selectTimeItem = command
      this.page = 1
      this.getList()
    },
    handleStatusCommand (command) {
      this.$xz('xz_substat_action', command.id)
      this.selectStatusItem = command
      this.page = 1
      this.getList()
    },
    close () {
      this.showInvoiceDialog = false
      this.getList()
    },
    closeContact () {
      this.showContactDialog = false
    },
    closeRecordDialog () {
      this.showRecordDialog = false
    },
    // 申请发票 || 发票记录
    invoiceClick (type) {
      if (type === 'select' && this.rightRowInfo?.row?.status === 6) {
        this.$PsMessage({ message: '抱歉，已退款订单不支持开发票' })
        return
      }
      if (type === 'select' && this.rightRowInfo?.row?.status !== 2) {
        this.$PsMessage({ message: '订单暂时未支付成功，请选择其他订单' })
        return
      }
      if (type === 'select' && this.rightRowInfo?.row?.invoice_status === 3) {
        // 发票记录弹窗
        this.showRecordDialog = true
        return
      }
      if (type === 'select' && [1, 2, 3, 7, 8].indexOf(this.rightRowInfo?.row?.invoice_status) > -1) {
        this.$PsMessage({ message: '该笔订单已申请过开发票' })
        return
      }
      if (type === 'select' && this.rightRowInfo?.row?.is_partner) {
        // 合作方提示弹窗
        this.showContactDialog = true
        return
      }
      if (type === 'select' && this.rightRowInfo?.row?.order_id) {
        this.invoiceForm = this.rightRowInfo.row
      } else {
        this.invoiceForm = {}
      }
      this.showInvoiceDialog = true
    },
    rightClick (row, column, event) {
      event.preventDefault() // 阻止鼠标右键默认行为
      this.rightRowInfo.row = row
      this.rightRowInfo.left = event.clientX
      this.rightRowInfo.top = event.pageY
      this.rightRowInfo.visible = true

      const self = this
      document.addEventListener('click', function (event) {
        if (!event.target.closest('#rightClickBox')) {
          self.rightRowInfo.visible = false
          self.rightRowInfo.row = {}
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
  .right-click-box {
    position: fixed;
    z-index: 2;

    .el-button{
      width: 100px;
      height: 30px;
      line-height: 30px;
      background: #4A4B4D;
      box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.16);
      border-radius: 6px 6px 6px 6px;
      padding: 0;
      border: 1px solid #5E5E5E;
      font-size: 12px;
      &:hover {
        border-color: rgba(0,187,215,0.3);
        color: #00BBD7;
      }
    }
  }
  .invoice-btn-bottom {
    position: fixed;
    right: 24px;
    bottom: 20px;
    color: #fff;
    font-size: 12px;
    padding: 0;
    &:hover {
        color: #00BBD7;
      }
  }
</style>
